package com.jxs.android.weiget;

import java.util.LinkedList;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jxs.android.ges.R;

public class WeilhActionBar extends RelativeLayout implements OnClickListener {

	private LayoutInflater mInflater;
	private ImageView mLogoView;
	private View mBackIndicator;
	// private View mHomeView;
	private TextView mTitleView;
	private LinearLayout mActionsView;
	private ImageButton mHomeBtn;
	private RelativeLayout mHomeLayout;
	private ProgressBar mProgress;

	public WeilhActionBar(Context context, AttributeSet attrs) {
		super(context, attrs);

		mInflater = LayoutInflater.from(context);

		LayoutInflater.from(context).inflate(R.layout.ptas_actionbar_img, this);

		mLogoView = (ImageView) findViewById(R.id.actionbar_home_logo);
		mHomeLayout = (RelativeLayout) findViewById(R.id.actionbar_home_bg);
		mHomeBtn = (ImageButton) findViewById(R.id.actionbar_home_btn);
		mBackIndicator = findViewById(R.id.actionbar_home_is_back);
		mProgress = (ProgressBar) findViewById(R.id.actionbar_progress);

		mTitleView = (TextView) findViewById(R.id.actionbar_title);
		mActionsView = (LinearLayout) findViewById(R.id.actionbar_actions);

		TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ActionBar);
		CharSequence title = a.getString(R.styleable.ActionBar_title);
		if (title != null) {
			setTitle(title);
		}
		a.recycle();
	}

	public void setHomeAction(Action action) {
		mHomeBtn.setOnClickListener(this);
		mHomeBtn.setTag(action);
		mHomeBtn.setImageResource(action.getDrawable());
		mHomeLayout.setVisibility(View.VISIBLE);
	}

	public void clearHomeAction() {
		mHomeLayout.setVisibility(View.GONE);
	}

	public void showProgressBar(boolean show) {
		if (show) {
			mProgress.setVisibility(View.VISIBLE);
		} else
			mProgress.setVisibility(View.GONE);
	}

	public void setProgressInVisible() {
		mProgress.setVisibility(INVISIBLE);
	}

	public void setActionSelected(View view) {

		for (int index = 1; index < mActionsView.getChildCount(); index++) {
			if (view.getTag().equals(mActionsView.getChildAt(index).getTag())) {
				view.setBackgroundResource(R.drawable.ptas_actionbar_btn_pressed);
			} else {
				mActionsView.getChildAt(index).setBackgroundResource(R.drawable.ptas_actionbar_btn_normal);
			}
		}
	}

	/**
	 * Shows the provided logo to the left in the action bar.
	 * 
	 * This is ment to be used instead of the setHomeAction and does not draw a
	 * divider to the left of the provided logo.
	 * 
	 * @param resId
	 *            The drawable resource id
	 */
	public void setHomeLogo(int resId) {
		// TODO: Add possibility to add an IntentAction as well.
		mLogoView.setImageResource(resId);
		mLogoView.setVisibility(View.VISIBLE);
		mHomeLayout.setVisibility(View.GONE);
	}

	public void setDisplayHomeAsUpEnabled(boolean show) {
		mBackIndicator.setVisibility(show ? View.VISIBLE : View.GONE);
	}

	public void setTitle(CharSequence title) {
		mTitleView.setText(title);
	}

	public void setTitle(int resid) {
		mTitleView.setText(resid);
	}

	/**
	 * Function to set a click listener for Title TextView
	 * 
	 * @param listener
	 *            the onClickListener
	 */
	public void setOnTitleClickListener(OnClickListener listener) {
		mTitleView.setOnClickListener(listener);
	}

	@Override
	public void onClick(View view) {
		final Object tag = view.getTag();
		if (tag instanceof Action) {
			final Action action = (Action) tag;
			action.performAction(view);
		}
	}

	/**
	 * Adds a list of {@link Action}s.
	 * 
	 * @param actionList
	 *            the actions to add
	 */
	public void addActions(ActionList actionList) {
		int actions = actionList.size();
		for (int i = 0; i < actions; i++) {
			addAction(actionList.get(i));
		}
	}

	/**
	 * Adds a new {@link Action}.
	 * 
	 * @param action
	 *            the action to add
	 */
	public void addAction(Action action) {
		final int index = mActionsView.getChildCount();
		addAction(action, index);
	}

	/**
	 * Adds a new {@link Action} at the specified index.
	 * 
	 * @param action
	 *            the action to add
	 * @param index
	 *            the position at which to add the action
	 */
	public void addAction(Action action, int index) {
		mActionsView.addView(inflateAction(action), index);
	}

	public View getAction(int index) {
		if (index <= mActionsView.getChildCount()) {
			return mActionsView.getChildAt(index);
		}
		return null;
	}

	/**
	 * Removes all action views from this action bar
	 */
	public void removeAllActions() {
		mActionsView.removeAllViews();
	}

	/**
	 * Remove a action from the action bar.
	 * 
	 * @param index
	 *            position of action to remove
	 */
	public void removeActionAt(int index) {
		mActionsView.removeViewAt(index);
	}

	/**
	 * Remove a action from the action bar.
	 * 
	 * @param action
	 *            The action to remove
	 */
	public void removeAction(Action action) {
		int childCount = mActionsView.getChildCount();
		for (int i = 0; i < childCount; i++) {
			View view = mActionsView.getChildAt(i);
			if (view != null) {
				final Object tag = view.getTag();
				if (tag instanceof Action && tag.equals(action)) {
					mActionsView.removeView(view);
				}
			}
		}
	}

	/**
	 * Returns the number of actions currently registered with the action bar.
	 * 
	 * @return action count
	 */
	public int getActionCount() {
		return mActionsView.getChildCount();
	}

	/**
	 * Inflates a {@link View} with the given {@link Action}.
	 * 
	 * @param action
	 *            the action to inflate
	 * @return a view
	 */
	private View inflateAction(Action action) {
		View view = null;
		if (null == action) {
			return view;
		}
		if (null != action.getView()) {
			view = action.getView();
		} else {
			if ((Pattern.VALIDATE != action.getText() && Pattern.VALIDATE == action.getDrawable())
					|| (Pattern.VALIDATE == action.getText() && Pattern.VALIDATE == action.getDrawable())) {
				view = mInflater.inflate(R.layout.ptas_actionbar_item_txt, mActionsView, false);
				TextView labelView = (TextView) view.findViewById(R.id.actionbar_item_txt);
				if (Pattern.VALIDATE != action.getText()) {
					labelView.setText(action.getText());
				} else {
					labelView.setText(action.getTitle());
				}
			} else if (Pattern.VALIDATE != action.getDrawable() && Pattern.VALIDATE == action.getText()) {
				view = mInflater.inflate(R.layout.ptas_actionbar_item_img, mActionsView, false);
				ImageButton labelView = (ImageButton) view.findViewById(R.id.actionbar_item_img);
				labelView.setImageResource(action.getDrawable());
			}
		}
		if (null != view && null != action) {
			view.setTag(action);
			view.setOnClickListener(this);
		}
		return view;
	}

	/**
	 * A {@link LinkedList} that holds a list of {@link Action}s.
	 */
	@SuppressWarnings("serial")
	public static class ActionList extends LinkedList<Action> {
	}

	/**
	 * Definition of an action that could be performed, along with a icon to
	 * show.
	 */
	public interface Action {
		public int getDrawable();

		public int getText();

		public String getTitle();

		public View getView();

		public void performAction(View view);
	}

	public static abstract class AbstractAction implements Action {
		private int mDrawable;
		private int text;
		private String title;

		@Override
		public View getView() {
			// TODO Auto-generated method stub
			return null;
		}

		public AbstractAction() {
			super();
		}

		public AbstractAction(int drawable) {
			mDrawable = drawable;
			this.text = WeilhActionBar.Pattern.VALIDATE;
		}

		public AbstractAction(int drawable, int text) {
			mDrawable = drawable;
			this.text = text;
		}

		public AbstractAction(String title) {
			mDrawable = -1;
			this.text = -1;
			this.title = title;
		}

		@Override
		public int getDrawable() {
			return mDrawable;
		}

		@Override
		public int getText() {
			return text;
		}

		@Override
		public String getTitle() {
			return title;
		}

	}

	public static class IntentAction extends AbstractAction {
		private Context mContext;
		private Intent mIntent;

		public IntentAction(Context context, Intent intent, int drawble) {
			this(context, intent, drawble, WeilhActionBar.Pattern.VALIDATE);
		}

		public IntentAction(Context context, Intent intent, int drawble, int text) {
			super(drawble, text);
			mContext = context;
			mIntent = intent;
		}

		@Override
		public void performAction(View view) {
			try {
				mContext.startActivity(mIntent);
			} catch (ActivityNotFoundException e) {
				Log.e(mContext.getClass().getName(), e.getMessage(), e);
			}
		}
	}

	static public interface Pattern {
		public static final int VALIDATE = -1;
	}
}
